<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\member\model;

    use think\Db;
    use think\Model as ThinkModel;

    class MemberModel extends ThinkModel
    {

        // 设置当前模型对应的完整数据表名称
        protected $name = 'member';

        // 自动写入时间戳
        protected $autoWriteTimestamp = true;

        /**
         * @param array $map
         * @param array $order
         *
         * @return \think\Paginator
         * @throws \think\exception\DbException
         */
        static public function getMemberList($map = [],$order = [])
        {

            return Db::name('member m')->field('m.id,m.phone,mw.nickName,mw.avatarUrl,m.status')->join('wechat_member mw','m.id = mw.member_id','left')->where($map)->order($order)->paginate();

        }

        /**
         * 获取用户上一级
         *
         * @param int $id
         *
         * @return array|false|\PDOStatement|string|ThinkModel
         */
        static public function getMemberGroup($id = 0)
        {

            $map['m.id'] = $id;
            $data        = Db::name('member m')//
            ->field('m.phone_puid,grp.phone')//
            ->join('member grp','m.phone_puid = grp.id','left')//
            ->where($map)->find();

            if (empty($data))
            {
                $data['phone'] = '';
            }

            return $data;
        }

        /**
         * ID获取信息
         *
         * @param null $id
         *
         * @return array|null
         */
        static public function getOneByID($id = null,$field = '*')
        {

            if ($field == '*')
            {
                return isset($id) ? self::where(['id' => $id])->column('*') [$id] : null;
            }
            return isset($id) ? self::where(['id' => $id])->column($field) : null;
        }

        static public function getInfoByPhone($phone)
        {

            return Db::name('member')->whereLike('phone',"%{$phone}%")->column('id');
        }

        static public function checkSelfPhone($phone,$member_id)
        {

            return self::where(['phone' => $phone,'id' => $member_id])->find() ? true : '手机号错误！请输入您自己绑定的手机号码！';
        }

        static public function GetIDs($data_list,$field = 'member_id')
        {

            $member = [];
            foreach ($data_list as $item)
            {
                if (isset($item[$field]))
                {
                    $member[$item[$field]] = $item[$field];
                }

            }
            return self::whereIn('id',$member)->column('id,phone');
        }

        static public function GetIDsUrl($data_list,$field = 'member_id')
        {

            $Members = self::GetIDs($data_list,$field);
            foreach ($Members as $key => &$member)
            {
                $member = "<a href='" . url('/member/index/view/id/' . $key) . "'>$member</a>";
            }
            unset($member);

            return $Members;
        }

    }